package cn.imvc.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.Notice;
import cn.imvc.app.models.NoticeModel;
import cn.imvc.core.NavTabBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

class FormModel {
	private ViewRequest viewRequest;
	private int[] ids;
	private Notice notice;

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

}

@Controller
@RequestMapping("/noticen")
public class NoticenControllerImpl implements NoticeController {
	@Autowired
	NoticeModel noticeModel;

	@RequestMapping("/search")
	public String search(NoticeFormModel formModel,Model model) {
		model.addAttribute("viewRequest", formModel.getViewRequest());
		return "noticen/search";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String index(FormModel formModel, Model model) {
		Page<Notice> page = noticeModel.find(formModel.getViewRequest());
		model.addAttribute("page", page);
		model.addAttribute("viewRequest", formModel.getViewRequest());
		return "noticen/index";
	}

	@RequestMapping("/{id}")
	public String show(@PathVariable int id, Model model) {
		model.addAttribute("notice", noticeModel.get(id));
		return "noticen/show";
	}

	@RequestMapping("/new")
	public String editNew() {
		return "noticen/editNew";
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public String create(FormModel formModel) {
		noticeModel.save(formModel.getNotice());
		return Render.json(new NavTabBean("200", "创建成功", "closeCurrent"));
	}

	@RequestMapping("/{id}/edit")
	public String edit(@PathVariable int id, Model model) {
		model.addAttribute("notice", noticeModel.get(id));
		return "noticen/edit";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public String update(@PathVariable int id, FormModel formModel) {
		formModel.getNotice().setId(id);
		noticeModel.update(formModel.getNotice());
		return Render.json(new NavTabBean("200", "修改成功", "closeCurrent"));
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public String destroy(@PathVariable int id) {
		noticeModel.delete(id);
		return Render.json(new NavTabBean("200", "删除成功"));
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ResponseBody
	public String destroyBatch(FormModel formModel) {
		noticeModel.deleteBatch(formModel.getIds());
		return Render.json(new NavTabBean("200", "删除成功"));
	}

}
